package com.zhsm.fuxi.mvp.ui.dialog

import android.content.Context
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import com.rd.draw.data.Orientation
import com.zhsm.paipaiout.baselibrary.utils.ToastUtils
import com.zhsm.fuxi.R
import com.zhsm.fuxi.base.BaseDialog
import com.zhsm.fuxi.mvp.ui.adapter.TakePicVpAdapter
import com.zhsm.fuxi.utils.GlideUtils
import kotlinx.android.synthetic.main.dialog_take_photo.*
import org.jetbrains.anko.sdk25.coroutines.onClick


/**
 * Created by GuZhC on 2018/11/05.
 * describe:
 */
class ReportsTakeCarDialog(context: Context) : BaseDialog(context) {
    val mContext = context
    var nowPager = 0
    val images = listOf<ImageView>(ImageView(context), ImageView(context), ImageView(context))
    val imagesUrl = arrayListOf<String>("", "", "")
    val views: MutableList<View> = ArrayList()

    override fun getLayoutId(): Int {
        return R.layout.dialog_take_photo
    }

    override fun getGravity(): Int {
        return BaseDialog.GRAVITY_CENTER
    }

    override fun isCanceledOnTouchOutside(): Boolean {
        return true
    }

    override fun initView() {
        super.initView()
        (images[0] as ImageView).setImageResource(R.drawable.pic_left_u)
        (images[1] as ImageView).setImageResource(R.drawable.pic_right_u)
        (images[2] as ImageView).setImageResource(R.drawable.pic_back)
        views.addAll(images)
        views.add(LayoutInflater.from(context).inflate(R.layout.tlayout_takecat_hint, null))
        takePicViewpager.adapter = TakePicVpAdapter(views)
        takePicIndicatorView.setViewPager(takePicViewpager)
        takePicIndicatorView.setAutoVisibility(true)
        takePicIndicatorView.radius = 4
        takePicIndicatorView.setOrientation(Orientation.HORIZONTAL)
        setClick()
        dialogRoot.onClick { dismiss() }
    }

    fun setClick() {
        takePicHint.onClick {
            mOnClickBtn?.takePic()
        }
        takePicViewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                nowPager = position
                if (position > 0) takePicBtnLast.visibility = View.VISIBLE
                else takePicBtnLast.visibility = View.GONE
                when (position) {
                    0 -> {
                        takePicHint.visibility = View.VISIBLE
                        takePicHint.text = "左前方45度拍照"

                    }
                    1 -> {
                        takePicHint.visibility = View.VISIBLE
                        takePicHint.text = "右前方45度拍照"
                    }
                    2 -> {
                        takePicHint.visibility = View.VISIBLE
                        takePicHint.text = "正后方拍照"
                    }
                    3 -> {
                        takePicHint.visibility = View.GONE
                    }
                }
            }
        })
        takePicBtnNext.onClick {
            if (nowPager == 3) {
                mOnClickBtn?.oNnext()
            } else {
                takePicViewpager.currentItem = nowPager + 1
            }
        }
        takePicBtnLast.onClick { takePicViewpager.currentItem = nowPager - 1 }
    }

    fun saveImage(url: String, key: String) {
        imagesUrl[nowPager] = key
        GlideUtils.loadImage(mContext, url, images[nowPager])
    }

    fun isOk(): Boolean {
        imagesUrl.forEachIndexed { index, s ->
            if (s.isNullOrBlank()) {
                ToastUtils.makeText("请重新上传第${index + 1}张图片")
                return false
            }
        }
        return true
    }

    fun startCuntDown(timeRemaining: String) {
        takePicTime.text = "剩余 $timeRemaining"
    }
    override fun dismiss() {
        super.dismiss()
    }

    interface onClickBtn {
        //拍照
        fun takePic()

        //下一步
        fun oNnext()
    }

    private var mOnClickBtn: onClickBtn? = null

    fun setOnClickTakePhoto(mOnClickTakePhoto: onClickBtn?) {
        this.mOnClickBtn = mOnClickTakePhoto
    }

    fun clearDialog() {
        dismiss()
        nowPager = 0
        takePicViewpager.currentItem = nowPager
        imagesUrl[0] =""
        imagesUrl[1] =""
        imagesUrl[2] =""
        (images[0] as ImageView).setImageResource(R.drawable.pic_left_u)
        (images[1] as ImageView).setImageResource(R.drawable.pic_right_u)
        (images[2] as ImageView).setImageResource(R.drawable.pic_back)
    }
}